home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH16 / EX16_1B.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-03-27  |  5.9 KB  |  262 lines

  1. ; EX16_1a.asm
  2. ;
  3. ; A simple floating point calculator that demonstrates the use of the
  4. ; UCR Standard Library pattern matching routines.  Note that this
  5. ; program requires an FPU.
  6.  
  7.         .xlist
  8.         .386
  9.         .387
  10.         option        segment:use16
  11.         include     stdlib.a
  12.         includelib    stdlib.lib
  13.         matchfuncs
  14.         .list
  15.  
  16. ; If the symbol "DEBUG" is defined, then call the MatchSP routine
  17. ; to do stack overflow checking.  If "DEBUG" is not defined, just
  18. ; call the sl_Match2 routine directly.
  19.  
  20. DEBUG        =    0        ;Define for debugging.
  21.  
  22.         ifdef    DEBUG
  23. MatchPat    textequ    <MatchSP>
  24.         else
  25. MatchPat    textequ    <sl_Match2>
  26.         endif
  27.  
  28. dseg        segment    para public 'data'
  29.  
  30. ; The following is a temporary used when converting a floating point
  31. ; string to a 64 bit real value.
  32.  
  33. CurValue    real8    0.0
  34.  
  35.  
  36. ; A Test String:
  37.  
  38. TestStr        byte    "5+2-(3-1)",0
  39.  
  40.  
  41.  
  42.  
  43. ; Grammar for simple infix -> postfix translation operation:
  44. ; Semantic rules appear in braces.
  45. ;
  46. ; NOTE: This code has a serious problem.  The first production
  47. ; is left recursive and will generate an infinite loop.
  48. ;
  49. ; E -> E+T {print result} | T {print result}
  50. ; T -> <constant> {fld constant} | (E)
  51. ;
  52. ;
  53. ;
  54. ; UCR Standard Library Pattern that handles the grammar above:
  55.  
  56. ; An expression consists of an "E" item followed by the end of the string:
  57.  
  58. Expression      pattern {MatchPat,E,,EndOfString}
  59. EndOfString    pattern    {EOS}
  60.  
  61.  
  62. ; An "E" item consists of an "E" item optionally followed by "+" or "-"
  63. ; and a "T" item (E -> E+T | T):
  64.  
  65. E               pattern    {PatPrint,EMsg,,E2}
  66. EMsg        byte    "E->E+T | T",cr,lf,0
  67.  
  68. E2        pattern {MatchPat, E,T,Eplus}
  69. Eplus        pattern    {MatchChar, '+', T, epPlus}
  70. epPlus        pattern    {DoFadd,,,E3}
  71. E3        pattern    {PatPrint,EMsg3}
  72. EMsg3        byte    "E->E+T",cr,lf,0
  73.  
  74.  
  75. ; A "T" item is either a floating point constant or "(" followed by
  76. ; an "E" item followed by ")".
  77. ;
  78. ; The regular expression for a floating point constant is
  79. ;
  80. ;    [0-9]+ ( "." [0-9]* | ) ( ((e|E) (+|-| ) [0-9]+) | )
  81. ;
  82. ; Note: the pattern "Const" matches exactly the characters specified
  83. ;    by the above regular expression.  It is the pattern the calc-
  84. ;    ulator grabs when converting a string to a floating point number.
  85.  
  86.  
  87. Const           pattern {MatchPat, ConstStr, 0, FLDConst}
  88. ConstStr        pattern {MatchPat, DoDigits, 0, Const2}
  89. Const2        pattern    {matchchar, '.', Const4, Const3}
  90. Const3          pattern {MatchPat, DoDigits, Const4, Const4}
  91. Const4        pattern    {matchchar, 'e', const5, const6}
  92. Const5        pattern    {matchchar, 'E', Succeed, const6}
  93. Const6        pattern    {matchchar, '+', const7, const8}
  94. Const7        pattern    {matchchar, '-', const8, const8}
  95. Const8          pattern {MatchPat, DoDigits}
  96.  
  97. FldConst    pattern    {PushValue,,,ConstMsg}
  98. ConstMsg    pattern    {PatPrint,CMsg}
  99. CMsg        byte    "T->const",cr,lf,0
  100.  
  101. ; DoDigits handles the regular expression [0-9]+
  102.  
  103. DoDigits    pattern    {Anycset, Digits, 0, SpanDigits}
  104. SpanDigits    pattern    {Spancset, Digits}
  105.  
  106. ; The S production handles constants or an expression in parentheses.
  107.  
  108. T        pattern    {PatPrint,TMsg,,T2}
  109. TMsg        byte    "T->(E) | const",cr,lf,0
  110.  
  111. T2        pattern    {MatchChar, '(', Const, IntE}
  112. IntE            pattern {MatchPat, E, 0, CloseParen}
  113. CloseParen    pattern    {MatchChar, ')',,T3}
  114.  
  115. T3        pattern    {PatPrint,TMsg3}
  116. TMsg3        byte    "T->(E)",cr,lf,0
  117.  
  118.  
  119. ; The Succeed pattern always succeeds.
  120.  
  121. Succeed        pattern    {DoSucceed}
  122.  
  123.  
  124. ; We use digits from the UCR Standard Library cset standard sets.
  125.  
  126.         include    stdsets.a
  127.  
  128. dseg        ends
  129.  
  130.  
  131.  
  132. cseg        segment    para public 'code'
  133.         assume    cs:cseg, ds:dseg
  134.  
  135. ; Debugging feature #1:
  136. ; This is a special version of sl_Match2 that checks for
  137. ; stack overflow.  Stack overflow occurs whenever there
  138. ; is an infinite loop (i.e., left recursion) in a pattern.
  139.  
  140. MatchSP        proc    far
  141.         cmp    sp, offset StkOvrfl
  142.         jbe    AbortPgm
  143.         jmp    sl_Match2
  144.  
  145. AbortPgm:    print
  146.                 byte    cr,lf,lf
  147.         byte    "Error: Stack overflow in MatchSP routine.",cr,lf,0
  148.         ExitPgm
  149. MatchSP          endp
  150.  
  151.  
  152. ; PatPrint- A debugging aid.  This "Pattern matching function" prints
  153. ; the string that DS:SI points at.
  154.  
  155. PatPrint    proc    far
  156.         push    es
  157.         push    di
  158.         mov    di, ds
  159.         mov    es, di
  160.         mov    di, si
  161.         puts
  162.         mov    ax, di
  163.         pop    di
  164.         pop    es
  165.         stc
  166.         ret
  167. PatPrint    endp
  168.  
  169.  
  170.  
  171. ; DoSucceed matches the empty string.  In other words, it matches anything
  172. ; and always returns success without eating any characters from the input
  173. ; string.
  174.  
  175. DoSucceed    proc    far
  176.         mov    ax, di
  177.         stc
  178.         ret
  179. DoSucceed    endp
  180.  
  181.  
  182. ; DoFadd - Adds the two items on the top of the FPU stack.
  183.  
  184. DoFadd        proc    far
  185.         faddp    st(1), st
  186.         mov    ax, di            ;Required by sl_Match
  187.         stc                ;Always succeed.
  188.         ret
  189. DoFadd        endp
  190.  
  191.  
  192. ; PushValue-    We've just matched a string that corresponds to a
  193. ;        floating point constant.  Convert it to a floating
  194. ;        point value and push that value onto the FPU stack.
  195.  
  196. PushValue    proc    far
  197.         push    ds
  198.         push    es
  199.         pusha
  200.         mov    ax, dseg
  201.         mov    ds, ax
  202.  
  203.         lesi    Const        ;FP val matched by this pat.
  204.         patgrab            ;Get a copy of the string.
  205.         atof            ;Convert to real.
  206.         free            ;Return mem used by patgrab.
  207.         lesi    CurValue    ;Copy floating point accumulator
  208.         sdfpa            ; to a local variable and then
  209.         fld    CurValue    ; copy that value to the FPU stk.
  210.  
  211.         popa
  212.         mov    ax, di
  213.         pop    es
  214.         pop    ds
  215.         stc
  216.         ret
  217. PushValue    endp
  218.  
  219.  
  220.  
  221. ; The main program tests the expression evaluator.
  222.  
  223. Main        proc
  224.         mov    ax, dseg
  225.         mov    ds, ax
  226.         mov    es, ax
  227.         meminit
  228.  
  229.         finit            ;Be sure to do this!
  230.         fwait
  231.  
  232.         lesi    TestStr
  233.         puts            ;Print the expression
  234.  
  235.         ldxi    Expression
  236.         xor    cx, cx
  237.         match
  238.         jc    GoodVal
  239.         printff
  240.         byte    " is an illegal expression",cr,lf,0
  241.         ret
  242.  
  243. GoodVal:    fstp    CurValue
  244.         printff
  245.         byte    " = %12.6ge\n",0
  246.         dword    CurValue
  247.  
  248. Quit:        ExitPgm
  249. Main        endp
  250. cseg        ends
  251.  
  252. sseg        segment    para stack 'stack'
  253.         word    64 dup (?)        ;Buffer for stack overflow
  254. StkOvrfl    word    ?            ;Stack overflow if drops
  255. stk        db    1024 dup ("stack   ")    ; below StkOvrfl.
  256. sseg        ends
  257.  
  258. zzzzzzseg    segment    para public 'zzzzzz'
  259. LastBytes    db    16 dup (?)
  260. zzzzzzseg    ends
  261.         end    Main
  262.